import Dialog from '../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import * as Storage from '../../../../utils/storage';
const $api = require('../../../../request/api').API;
// pages/person/selectVillage/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    detailRole: '',
    userId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const detailRole = wx.getStorageSync(Storage.DETAIL_ROLE);
    this.setData({
      detailRole
    });
    if (options.userId) {
      this.setData({
        userId: options.userId
      });
    }
    this.getData();
    // if(detailRole==3){
    //   this.getVillageList()
    // }else if(detailRole==2){
    //   this.getData()
    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  /**
   *  清空全部数据
   */
  /**
   *  新增
   */
  add(e) {
    wx.navigateTo({
      url: '/subcontract/person/pages/addVillage/index?userId=' + this.data.userId
    });
  },

  async getData() {
    const { userId } = this.data;
    const params = {
      pageNo: 1,
      pageSize: 1000,
      userId
    };
    const res = await $api.getUserStackList(params);
    let list = res.data.data.list || [];
    this.setData({
      list
    });
  },
  async del(e) {
    console.log(e);
    const { stackId } = e.currentTarget.dataset.item;
    await Dialog.confirm({
      title: '提示',
      message: '确定删除吗？'
    });
    const params = {
      id: stackId,
      state: -1
    };
    await $api.delUserStack(params);
    Toast.success('删除成功');
    this.getData();

    const { index } = e.currentTarget.dataset;
    const { list } = this.data;
    list.splice(index, 1);
    this.setData({
      list
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
